import { modelOptions, prop, getModelForClass } from '@typegoose/typegoose'
import { Types } from 'mongoose'
import { ApiProperty, PickType } from '@nestjs/swagger'
import { Length } from 'class-validator'

@modelOptions({
  schemaOptions: {
    collection: 'permissions', //数据库集合的名称
    // 时间填充
    timestamps: true,
  },
})
export class PermissionClass {
  @ApiProperty({
    description: '父id',
    example: '23***',
    required: true,
  })
  @prop()
  public pid?: Types.ObjectId

  /** 许可的名称,方便后台进行权限授权 */
  @prop({ required: true, trim: true })
  @ApiProperty({
    description: '权限许可的名称',
    example: '全部数据',
    required: true,
  })
  @Length(2, 20)
  public name: string

  @ApiProperty({
    description: 'menu',
    example: '导航菜单的英文名',
    required: false,
  })
  @prop()
  public code?: string

  @ApiProperty({
    description: 'toCode',
    example: '',
    required: false,
  })
  @prop()
  public toCode?: string

  @ApiProperty({
    description: 'type',
    example: '',
    required: false,
  })
  @prop()
  public type?: number

  @ApiProperty({
    description: '状态',
    example: 1,
    required: false,
  })
  @prop()
  public status?: number

  @ApiProperty({
    description: '层级',
    example: 1,
    required: false,
  })
  @prop({ required: true })
  public level: number

  @ApiProperty({
    description: '是否选择',
    example: false,
    required: false,
  })
  @prop({ default: false })
  public select?: boolean
}

export const PermissionModel = getModelForClass(PermissionClass)

export class UpdatePermissionDto extends PickType(PermissionClass, [
  'name',
  'code',
  'pid',
  'level',
] as const) {}
